package com.qdsg.ysg.doctor.nurse.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.ui.adapter.AcceptOrderAdapter;
import com.qdsg.ysg.doctor.ui.adapter.WaitOrderAdapter;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.DocOrderListVO;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.LogUtil;
import jz.joyoung.robot.util.UtilList;

/**
 * @time 2021/4/12 15:12
 */
public class AtyNurseOrderList extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.cl_empty)
    ConstraintLayout clEmpty;

    int current = 1, totalPage;
    Boolean isLoadMore = true;//能不能继续加载更多

    List<DocOrderListVO.AcceptOrderList> mAcceptList = new ArrayList<>();
    List<DocOrderListVO.AcceptOrderList> mWaitList = new ArrayList<>();

    private String fromWhere;
    private AcceptOrderAdapter acceptOrderAdapter;
    private WaitOrderAdapter waitOrderAdapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_nurse_base_list;
    }

    @Override
    protected void initViews() {

        LinearLayoutManager manager = new LinearLayoutManager( mContext );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        recyclerView.setLayoutManager( manager );
        if ("accept".equals( fromWhere )) {
            tvTitle.setText( "已接单" );
            acceptOrderAdapter = new AcceptOrderAdapter( mContext, mAcceptList );
            recyclerView.setAdapter( acceptOrderAdapter );
            acceptOrderAdapter.setOnItemClickLitener( (position, conclusion) -> updateDocSetOut( mAcceptList.get( position ).isSetOut, mAcceptList.get( position ).docAppointmentId, conclusion ) );
        } else if ("wait".equals( fromWhere )) {
            tvTitle.setText( "待接单" );
            waitOrderAdapter = new WaitOrderAdapter( mContext, mWaitList );
            recyclerView.setAdapter( waitOrderAdapter );
        }
//        refreshLayout.setPrimaryColors( 0xffffffff,  0xff5481FA);
        refreshLayout.setEnableLoadMore( true );

        refreshLayout.setOnRefreshListener( refreshlayout -> {
            refreshLayout.finishRefresh();
            refreshLayout.setNoMoreData( false );//复原状态
            current = 1;
            refreshData();

        } );
        //加载更多
        refreshLayout.setOnLoadMoreListener( refreshLayout -> {
            LogUtil.e( "总数---" + totalPage );
            if (current >= totalPage) {
                refreshLayout.finishLoadMore();
                refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                //refreshLayout.setNoMoreData(false);

                ToastUtil.showToast( mContext, "数据全部加载完毕" );
            } else {
                loadMoreData();

            }
        } );


    }

    private void refreshData() {
        if ("accept".equals( fromWhere )) {
            getOrderList();
        } else if ("wait".equals( fromWhere )) {
            getWaitList();
        }
    }

    private void loadMoreData() {
        if ("accept".equals( fromWhere )) {
            getOrderListMore();
        } else if ("wait".equals( fromWhere )) {
            getWaitListMore();
        }

    }


    @Override
    protected void initData() {
        fromWhere = getIntent().getStringExtra( "fromWhere" );

//        patientId = getIntent().getStringExtra( "patientId" );
//        queryCheckList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }

    // 已接单列表
    private void getOrderList() {
        RestProxy.getInstance().getOrderList( current + "", new Observer<DocOrderListVO>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(DocOrderListVO orderListVO) {
                try {
                    totalPage = Integer.parseInt( orderListVO.data.pages );
                    List<DocOrderListVO.AcceptOrderList> list = orderListVO.data.records;
                    if (!UtilList.isEmpty( list )) {
                        refreshLayout.setVisibility( View.VISIBLE );
                        clEmpty.setVisibility( View.GONE );
                        mAcceptList.clear();
                        mAcceptList.addAll( list );
                        acceptOrderAdapter.notifyDataSetChanged();
                    } else {
                        refreshLayout.setVisibility( View.GONE );
                        clEmpty.setVisibility( View.VISIBLE );
                    }


                } catch (Exception e) {
                    // 捕获异常，如果要捕获Json数据异常，写catch(JSONException e)
                    e.printStackTrace();
                    //打印出来就Log.e(TAG,Log.getStackTraceString(e));
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                refreshLayout.finishRefresh( false );
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh( true );
            }
        } );
    }


    private void getOrderListMore() {
        current += 1;
        RestProxy.getInstance().getOrderList( current + "", new Observer<DocOrderListVO>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(DocOrderListVO orderListVO) {
                List<DocOrderListVO.AcceptOrderList> list = orderListVO.data.records;
                if (!UtilList.isEmpty( list )) {
                    mAcceptList.addAll( list );
                    acceptOrderAdapter.notifyDataSetChanged();
                    refreshLayout.finishLoadMore();
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onComplete() {
                refreshLayout.finishLoadMore();
            }
        } );
    }

    // @menu: APP-社区检查 申请单列表
    private void getWaitList() {
        RestProxy.getInstance().getWaitList( current + "", new Observer<DocOrderListVO>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(DocOrderListVO orderListVO) {
                try {
                    totalPage = Integer.parseInt( orderListVO.data.pages );
                    List<DocOrderListVO.AcceptOrderList> list = orderListVO.data.records;
                    if (!UtilList.isEmpty( list )) {
                        refreshLayout.setVisibility( View.VISIBLE );
                        clEmpty.setVisibility( View.GONE );
                        mWaitList.clear();
                        mWaitList.addAll( list );
                        waitOrderAdapter.notifyDataSetChanged();
                    } else {
                        refreshLayout.setVisibility( View.GONE );
                        clEmpty.setVisibility( View.VISIBLE );
                    }


                } catch (Exception e) {
                    // 捕获异常，如果要捕获Json数据异常，写catch(JSONException e)
                    e.printStackTrace();
                    //打印出来就Log.e(TAG,Log.getStackTraceString(e));
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                refreshLayout.finishRefresh( false );
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh( true );
            }
        } );
    }


    private void getWaitListMore() {
        current += 1;
        RestProxy.getInstance().getWaitList( current + "", new Observer<DocOrderListVO>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(DocOrderListVO orderListVO) {
                List<DocOrderListVO.AcceptOrderList> list = orderListVO.data.records;
                if (!UtilList.isEmpty( list )) {
                    mWaitList.addAll( list );
                    waitOrderAdapter.notifyDataSetChanged();
                    refreshLayout.finishLoadMore();
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onComplete() {
                refreshLayout.finishLoadMore();
            }
        } );
    }


    private void updateDocSetOut(String type, String docAppointmentId, String conclusion) {
        RestProxy.getInstance().updateDocSetOut( type, docAppointmentId, conclusion, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onNext(BaseResponse baseResponse) {
                current = 1;
                getOrderList();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
            }

            @Override
            public void onComplete() {
            }
        } );
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}

